package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.util.PageBean;

import javax.annotation.Resource;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private TbSpecificationMapper tbSpecificationMapper;
    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public PageBean<TbSpecification> getinfo(Integer number,Integer pageSize,String sname) {
        PageBean<TbSpecification> b = new PageBean<>();
        b.setPageNum(number);
        b.setPageSize(pageSize);
        List<TbSpecification> tlist = tbSpecificationMapper.selectByName(sname);
        b.setTotal(tlist.size());
        List<TbSpecification> list = tbSpecificationMapper.getinfo(b,sname);
        b.setResult(list);
        return b;
    }

    @Override
    public Integer addinfo(String specName,List<TbSpecificationOption> list) {
        TbSpecification t = new TbSpecification();
        t.setSpecName(specName);
        Integer i = tbSpecificationMapper.insertSelective(t);
        Long specId = t.getId();
        Integer m = tbSpecificationOptionMapper.addinfo(list,specId);
        return m;
    }

    @Override
    public Integer deleteAll(Long[] ids) {
        Integer t = tbSpecificationMapper.deleteAll(ids);
        Integer o = tbSpecificationOptionMapper.deleteAll(ids);
        return o;
    }

    @Override
    public TbSpecification toUpdate(Long id) {
        return tbSpecificationMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateinfo(String specName, List<TbSpecificationOption> list,Long id) {
        TbSpecification t = new TbSpecification();
        t.setId(id);
        t.setSpecName(specName);
        Integer i = tbSpecificationMapper.updateByPrimaryKeySelective(t);
        Integer d = tbSpecificationOptionMapper.deleteBySid(id);
        Integer m = tbSpecificationOptionMapper.addinfo(list,id);
        return m;
    }

    @Override
    public List<TbSpecification> getAll() {
        return tbSpecificationMapper.selectByExample(null);
    }
}
